import { _decorator, Component, Node, Prefab, instantiate, UITransform } from 'cc';
const { ccclass, property } = _decorator;
import methods from "db://assets/common/methods";
import config from "db://assets/common/config"

@ccclass('enemy')
export class enemy extends Component {
    @property(Prefab)
    enemy_Node: Prefab = null
    enemy_item_Node: Map<string, Node> = new Map()
    win_w: number = config.win_w;
    win_h: number = config.win_h;

    start() {
        for (let i = 1; i <= 100; i++) {
            let uuid = methods.getUUID()
            this.enemy_item_Node.set(uuid, instantiate(this.enemy_Node))
            // 获取节点的UITransform组件并设置宽高
            let enemyNode = this.enemy_item_Node.get(uuid);
            enemyNode.getComponent(UITransform).width = 50;
            enemyNode.getComponent(UITransform).height = 50;
            this.enemy_item_Node.get(uuid).x = methods.getRandomNumber(100, this.win_w - 100)
            this.enemy_item_Node.get(uuid).y = methods.getRandomNumber(this.win_h, this.win_h / 2)
            // @ts-ignore
            this.enemy_item_Node.get(uuid).enemyAttr = {
                isAttacked: false,
                hp: 1000,
            }
            this.enemy_item_Node.get(uuid).setParent(this.node)
        }
    }

    // 随机获取一个敌人节点（无需转换整个数组）
    getRandomEnemy(): [string, Node] | undefined {
        const size = this.enemy_item_Node.size;
        if (size === 0) return undefined;

        // 收集所有未被攻击的敌人（isAttacked: false）及其 UUID
        const availableEntries: [string, Node][] = [];

        // 遍历 Map 中的所有敌人节点
        for (const [uuid, node] of this.enemy_item_Node.entries()) {
            // @ts-ignore
            if (node.enemyAttr && !node.enemyAttr.isAttacked) {
                availableEntries.push([uuid, node]);
            }
        }

        // 如果有未被攻击的敌人，随机返回一个
        if (availableEntries.length > 0) {
            const randomIndex = Math.floor(Math.random() * availableEntries.length);
            return availableEntries[randomIndex];
        }

        // 如果所有敌人都被攻击过，则随机返回一个普通敌人
        const randomIndex = Math.floor(Math.random() * size);
        const iterator = this.enemy_item_Node.entries();

        for (let i = 0; i <= randomIndex; i++) {
            const result = iterator.next();
            if (result.done) break;
            return result.value;
        }

        return undefined; // 理论上不会执行到这里，因为 size > 0
    }

    // 定义计时器变量（需在类中声明）
    private timer: number = 0;
    update(deltaTime: number) {
        // 累加计时器
        this.timer += deltaTime;
        // 当计时器超过间隔时间时执行逻辑
        if (this.timer >= config.fps) {
            // 执行你的逻辑（每秒一次）
            this.doSomething();

            // 重置计时器（或减去间隔时间以保持精度）
            this.timer = 0;
            // 或：this.timer -= this.interval; // 更精确，但可能累积误差
        }
    }

    private doSomething() {
        // 每秒执行一次的代码
        for(let i of this.enemy_item_Node) {
            if(this.enemy_item_Node.get(i[0]).enemyAttr.hp <= 0) {
                i[1].destroy()
                this.enemy_item_Node.delete(i[0])
                continue;
            }
            i[1].y-=0.1
        }
    }
}